package com.sheng.sign.action;

import java.util.Collections;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sheng.sign.entity.Classes;
import com.sheng.sign.entity.Student;
import com.sheng.sign.service.ClassesService;
import com.sheng.sign.service.StudentService;

public class StudentAction extends ActionSupport implements ModelDriven<Student>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8455019250944966868L;
	protected String forwardView;
	protected static final String SUCCESS_JSP = "/success.jsp";
	protected static final String allSign_ADD = "/page/allSign_add.jsp";
	protected static final String ADD_JSP = "/page/Student_add.jsp";
	protected static final String EDIT_JSP = "/page/Student_edit.jsp";
	protected static final String LIST_JSP = "/page/Student_list.jsp";
	
	private List<Student> lists ;
	private List<Classes> ClassesList ;
	private List<Student> listss ;
	private Student student = new Student();
	private StudentService studentService;
	private ClassesService classesService;
	
	
	@Override
	public Student getModel() {
		return student;
	}
	
	public String openAdd() {
		ClassesList = classesService.findAllList();//存取班级列表
		for(int i=0;i<ClassesList.size();i++) {
			System.out.println("===班级列表==="+ClassesList.get(i).getClassesName());
		}
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	public String openEdit(){
		ClassesList = classesService.findAllList();//存取班级列表
		student = studentService.findById(student);
		System.out.println("==来到了openEditAction===值："+student.getId());
			setForwardView(EDIT_JSP);
			return SUCCESS;
	}
	 
	/**
	 * 
	 * @Description:TODO
	 * @return
	 * String
	 * @exception:
	 * @author: huangshengjun 1532950421@qq.com
	 * @time:2019年12月7日 
	 */
	public String add() {
		student.setIsDelete(0);
		System.out.println("==来到了addAction== "+student.toString());
		boolean value = studentService.add(student);
		if(value){
			setForwardView(LIST_JSP);
			return this.list();
		}else{
			return ERROR;
		}
	}
	
	public String edit() {
		System.out.println("==来到了editAction== "+student);
		boolean value = studentService.edit(student);
		if(value){
			return this.list();
		}else{
			return ERROR;
		}
	}
	
	public String delete() {
		student = studentService.findById(student);
		System.out.println("==来到了delAction== "+student);
		boolean value =studentService.delete(student);
		if(value){
			return this.list();
		}else{
			return ERROR;
		}
	}
	/**
	 * 
	 * @Description:根据班级id+课程id查找
	 * @return
	 * String
	 * @exception:
	 * @author: huangshengjun 1532950421@qq.com
	 * @time:2019年12月7日 下午8:14:02
	 */
	public String listByClass() {
		lists = studentService.listByClass(student);
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description:全部信息
	 * @return
	 * String
	 * @exception:
	 * @author: huangshengjun 1532950421@qq.com
	 * @time:2019年12月7日 下午8:15:05
	 */
	public String list() {
		lists = studentService.findAllList();
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	

	
	
	//**********上面是学生的操作逻辑方法*************--我是分割线--************下面是考勤方法******************//
	
	
	
	
	
	/**
	 * 从签到考勤的选择页面过来
	 * @Description:TODO描述
	 * @return
	 * String
	 * @exception:
	 * @author: huangshengjun 1532950421@qq.com
	 * @time:2019年12月7日 下午8:18:58
	 */
	public String findByCourseIdClassesId() {
		lists = studentService.listByClass(student);
		setForwardView(allSign_ADD);//选择后的班级学生信息页面，就开始进行签到操作
		return SUCCESS;
	}
	
	
	/**
	 * 进行班级选择后随机抽取3名同学进行考勤,使用Collections工具类
	 * @Description:TODO描述
	 * @return
	 * String
	 * @exception:
	 * @author: huangshengjun 1532950421@qq.com
	 * @time:2019年12月8日 下午9:14:54
	 */
	public String findByRandom() {
		listss = studentService.listByClass(student);
		//Collections工具类
		Collections.shuffle(listss);
		//选择随机数量
	    int randomSeriesLength = 3;
	    //写入随机抽取的数组
	    lists = listss.subList(0, randomSeriesLength);
	    //选择后的班级学生信息页面，就开始进行签到操作
		setForwardView(allSign_ADD);
		return SUCCESS;
	}

	public String getForwardView() {
		return forwardView;
	}

	public void setForwardView(String forwardView) {
		this.forwardView = forwardView;
	}

	public List<Student> getLists() {
		return lists;
	}

	public void setLists(List<Student> lists) {
		this.lists = lists;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public List<Classes> getClassesList() {
		return ClassesList;
	}

	public void setClassesList(List<Classes> classesList) {
		ClassesList = classesList;
	}

	public ClassesService getClassesService() {
		return classesService;
	}

	public void setClassesService(ClassesService classesService) {
		this.classesService = classesService;
	}

	public List<Student> getListss() {
		return listss;
	}

	public void setListss(List<Student> listss) {
		this.listss = listss;
	}

}
